<?php class receive_record extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    
    function truncate(){
    	return $this->db->truncate('receive_record');
    }
    
    
    function insert($arr){
       $result = $this->db->insert('receive_record',$arr);
       return $result;
    }
    
    
    function select_totalcount_chart($start,$end){
       $sql = "select sum(count) as count from receive_record ";
       if($start!=''){
         $sql.= " where createtime between ".$start." and ".$end ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    function select_categorycount_chart($start,$end){
    $sql = "select DISTINCT category_id from receive_record ";
       if($start!=''){
         $sql.= " where createtime between ".$start." and ".$end ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    function getreceive_array($arr){
       $this->db->limit(1);
       $this->db->where($arr);
       $this->db->order_by("receive_id", "desc");
       $query = $this->db->get('receive_record');
       $result = $query->result(); 
       return $result;
    }
    
    
    function main_select_update($start,$end){

       $sql = "select DISTINCT category_id from receive_record where is_update!=0 ";
       if($start!=''){
         $sql.= " and createtime between ".$start." and ".$end ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where($id);
       }
       $this->db->order_by("receive_id", "desc");
       $query = $this->db->get('receive_record');
       $result = $query->result(); 
       return $result;
    }
    
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$version_id,'version_id'=>$id));
       $this->db->select_sum('count');
       $query = $this->db->get('receive_record');
       $result = $query->result(); 
       return $result;
    }
    
    function update($id){
       $this->version_id = $_POST['version_id'];
       $this->category_id = $_POST['category_id'];
       $this->mod_id = $_POST['mod_id'];
       $this->count = $_POST['count'];
       if(isset($_POST['codes'])){
         $this->codes = $_POST['codes'];
       }
       if(!isset($_POST['contacter'])){
         $this->contacter = $_POST['contacter'];
       }
       $result = $this->db->update('receive_record',$this,array('r_id'=>$id));
       return $result;
    }
    
    function update_ar($array,$id){
       $result = $this->db->update('receive_record',$array,array('receive_id'=>$id));
       return $result;
    }
    
    function update_array($where,$version,$array){
       $this->db->where($where);
       $this->db->where('version_id !=',$version);
       $result = $this->db->update('receive_record',$array);
     
       return $result;
    }
    
 function getcount($id,$version_id=''){
        $where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('receive_record');
        $result = $query->result(); 
        return $result;
    }
    
    function del($id){
       $result = $this->db->delete('receive_record',array('receive_id'=>$id));
       return $result;
    }
}